package com.gopay.bis.web.controller.ebank;

import java.math.BigDecimal;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.bis.common.util.TransUtils;
import com.gopay.bis.domain.order.OrderInfo;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.constants.bank.UmPayConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.umpay.ebank.UmPayMessage;

/**
 * 联动优势网银直连接口前台通知处理类
 * 
 * @UmPayFrontRes.java
 * @author zhengxue
 * @2013-12-4 下午4:11:00 www.gopay.com.cn Inc.All rights reserved.
 */
@Controller
@Scope("prototype")
public class UmPayFrontRes extends BankResBase {
	
	private static Log logger = LogFactory.getLog(UmPayFrontRes.class);

	@RequestMapping(value = "/umPayFrontRes")
	protected ModelAndView execute(HttpServletRequest request,HttpServletResponse response) {
		logger.error("--------------- UmPayFrontRes bankback  ---------------");
		return super.execute(request, response);
	}

	@Override
	protected BaseMessage buildMessage(HttpServletRequest request) {
		UmPayMessage msg = new UmPayMessage();
		// 生产网银直连接口验签对象
		Map<String, String> ht = new HashMap<String, String>();
		String name = "";
		String values = "";
		for (Enumeration names = request.getParameterNames();names.hasMoreElements(); ht.put(name, values)) {
			name = (String) names.nextElement();
			values = request.getParameter(name);
		}
		msg.setBank(BankConstants.BANK_LOGO_UMPAY);
		msg.setReqMap(ht);
		msg.setBankOrderId(request.getParameter("order_id"));
		msg.setOrderId(request.getParameter("order_id"));
	    msg.setAmount(new BigDecimal(request.getParameter("amount")));
		msg.setTradeState(request.getParameter("trade_state"));
		msg.setFront(true);
        //银行业务产品00个人网银支付
        msg.setBankBusProd(BisBankBusinessProd._00);
		return msg;
	}

	@Override
	protected OrderInfo buildOrderInfo(HttpServletRequest request, Result result) {
		logger.error("UMPAY FRONT:"+ ToStringBuilder.reflectionToString((UmPayMessage) result.getModel("message")));
		OrderInfo orderInfo = new OrderInfo();
		UmPayMessage umpMessage = (UmPayMessage) result.getModel("message");
		if(umpMessage!=null){
			String trade_state = request.getParameter("trade_state");
			String amt = TransUtils.amountConvert2Yuan(request.getParameter("amount"));
			if (trade_state != null&& trade_state.equals(UmPayConstants.TRADE_SUCCESS)) {
				orderInfo.setOrderId(umpMessage.getOrderId());
				orderInfo.setAmt(amt);
				orderInfo.setNotifyNum("1");
				orderInfo.setPayStatus("00");
				orderInfo.setRetCode("00");
				orderInfo.setRetMsg("success");
			} else {
				orderInfo.setOrderId(umpMessage.getOrderId());
				orderInfo.setAmt(amt);
				orderInfo.setNotifyNum("1");
				orderInfo.setPayStatus("01");
				orderInfo.setRetCode("01");
				orderInfo.setRetMsg("");
			}
		}
		orderInfo.setCardType("");// 信用卡类型
		orderInfo.setBankName("umpay");
		return orderInfo;
	}

	@Override
	protected boolean checkParameters(HttpServletRequest request) {
		if (StringUtils.isEmpty(request.getParameter("order_id"))
				|| StringUtils.isEmpty(request.getParameter("amount"))
				|| StringUtils.isEmpty(request.getParameter("trade_state"))) {
		return false;
		}
		return true;
	}
}
